define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/store/index' + location.search,
                    add_url: 'store/store/add',
                    edit_url: 'store/store/edit',
                    del_url: 'store/store/del',
                    multi_url: 'store/store/multi',
                    //import_url: 'store/store/import',
                    table: 'store',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='district_id']", form).addClass("selectpage")
                    .data("source", "district/get_district")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'district_id', title: __('District_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'logo', title: __('Logo'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'ticket', title: __('支持票数'), operate: false, sortable: true},
                        {field: 'money', title: __('支持总金额(元)'), operate: false, sortable: true},
                        {field: 'user_count', title: __('用户数'), operate: false},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'if_ticket',
                            title: __('If_ticket'),
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle, searchList:{0:"关闭", 1:"开启"}
                        },
                        //{field: 'if_ticket', title: __('If_ticket'), formatter: Table.api.formatter.status, searchList:{0:"关闭", 1:"开启"}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'up',
                                    icon: 'fa fa-list',
                                    title: '支持记录',
                                    text: '支持记录',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    url: 'store/store/orderlist?store_id={id}',
                                    hidden: function(row, value, index){
                                        if(row.ticket == 0){
                                            return true;
                                        }
                                        return false;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var city_id = 28141;
            $("input[name='district_id']").data('params', function(){
                if(!city_id){
                    city_id = -1;
                }
                return {custom:{parent_id:city_id}, type:1};
            });
        },
        orderlist: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/store/orderlist' + location.search,
                    //import_url: 'store/store/import',
                    table: 'store',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'pay_time',
                columns: [
                    [
                        {field: 'store.name', title: __('分会名称'), operate: false},
                        {field: 'user.nickname', title: __('用户名称'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('头像'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'money', title: __('支持金额'), operate: false, sortable: true},
                        {field: 'pay_time', title: __('支付时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var city_id = 28141;
            $("input[name='district_id']").data('params', function(){
                if(!city_id){
                    city_id = -1;
                }
                return {custom:{parent_id:city_id}, type:1};
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $("#c-district_id").data('params', function(){
                    return {type: 1};
                });
            }
        }
    };
    return Controller;
});